package com.shizhanzhe.zxbapp.customtask;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.CreditCardBean;
import com.shizhanzhe.zxbapp.bean.ScreenTaskBean;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.home.SourceMaterialActivity;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.task.WebActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.Util;
import com.shizhanzhe.zxbapp.widget.CountdownTextView;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreditCardDetailsActivity extends AppCompatActivity {

    @BindView(R.id.iv_chakan)
    ImageView ivChakan;
    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.backarea)
    RelativeLayout backarea;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.iv_img)
    ImageView iv_img;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_chakan)
    TextView tvChakan;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_quxiao)
    TextView tvQuxiao;
    @BindView(R.id.tv_time)
    CountdownTextView tvTime;
    @BindView(R.id.ll_shangchuan)
    LinearLayout llShangchuan;
    @BindView(R.id.ll_jinxing)
    LinearLayout llJinxing;
    @BindView(R.id.gourl)
    TextView gourl;
    @BindView(R.id.iv_isvip)
    ImageView iv_isvip;
    @BindView(R.id.tv_vip)
    TextView tv_vip;
    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.save)
    ImageView save;
    private CreditCardAdapter mAdapter;
    private int taskid = 0;
    private int type = 3;
    private CreditCardBean card;
    private int isvip = 0;
    private int again = 0; //1确定放弃上个任务

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_card_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorAccent).init();
        taskid = getIntent().getIntExtra("taskid", 0);
        //item = (ScreenBean.DataBeanX.DataBean) getIntent().getExtras().getSerializable("item");
        type = getIntent().getIntExtra("type", 3);
        topTitle.setText("任务详情");
        initData();
    }

    private void initData() {
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CreditCardAdapter(this);
//        recycler.addItemDecoration(new TimelineDecoration(getDimen(R.dimen.dp30),
//                getDimen(R.dimen.dp15),
//                ContextCompat.getDrawable(this, R.drawable.ic_lens_black_12dp),
//                getDimen(R.dimen.dp15),
//                1));
        recycler.setAdapter(mAdapter);

        cardlist();
    }

    private void cardlist() {
        //获取信用卡任务详情
        OkHttpDownloadJsonUtil.downloadJson(this, Path.CREDITCARDPUSH(UserBean.uid, taskid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
//                Log.i("________", json);
                try {
                    gourl.setVisibility(View.VISIBLE);
                    Gson gson = new Gson();
                    card = gson.fromJson(json, CreditCardBean.class);
                    tvText.setText(card.getData().getTitle());
                    tvMoney.setText(card.getJin() + "元");
                    tvType.setText("任务类型：信用卡");
                    isvip = card.getData().getIsvip();
                    if (UserBean.level==0){
                        tvMoney.setText(card.getJin()+"元");
                        tv_vip.setText((card.getJin()/Double.parseDouble(UserBean.carddivide))*(Double.parseDouble(UserBean.vipcarddivide)*10)/10+ "元");
                    }else {
                        tv_vip.setText(card.getJin()+"元");
                        tvMoney.setText(card.getJin()/Double.parseDouble(UserBean.vipcarddivide)*Double.parseDouble(UserBean.carddivide)+ "元");
                        Log.i("_____________",card.getJin()/Double.parseDouble(UserBean.vipcarddivide)+"_"+Double.parseDouble(UserBean.carddivide));
                    }
                    if (isvip == 1) {
                        iv_isvip.setVisibility(View.VISIBLE);
                    } else {
                        iv_isvip.setVisibility(View.GONE);
                    }
                    Glide.with(CreditCardDetailsActivity.this).load(Path.img(card.getData().getLogo())).apply(new RequestOptions().error(R.mipmap.logo)).into(iv_img);
                    gourl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(CreditCardDetailsActivity.this, WebActivity.class);
                            intent.putExtra("url", card.getData().getUrl());
                            intent.putExtra("fen", "");
                            startActivity(intent);
                        }
                    });
                    mAdapter.updateData(card.getStep());
                    mAdapter.setListener(new CreditCardAdapter.ImageClickListener() {
                        @Override
                        public void imageClick(final String url) {
                            fl.setVisibility(View.VISIBLE);
                            Glide.with(CreditCardDetailsActivity.this).load(Path.BASE_URL + url).into(ivChakan);
                            fl.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View view) {
                                    List<String> list = new ArrayList<>();
                                    list.add(url);
                                    Util.GetImageInputStream(CreditCardDetailsActivity.this, list);
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    Toast.makeText(CreditCardDetailsActivity.this, "图片已保存", Toast.LENGTH_SHORT).show();
                                    return false;
                                }
                            });
                            save.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    List<String> list = new ArrayList<>();
                                    list.add(url);
                                    Util.GetImageInputStream(CreditCardDetailsActivity.this, list);
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    Toast.makeText(CreditCardDetailsActivity.this, "图片已保存", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });

                    //5未领取任务   0已领取   1已提交
                    switch (card.getStatus()) {
                        case 5:
                            tvCommit.setVisibility(View.GONE);
                            llJinxing.setVisibility(View.VISIBLE);
                            tvChakan.setVisibility(View.GONE);
                            tvQuxiao.setVisibility(View.GONE);
                            tvTime.setVisibility(View.GONE);
                            break;
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                            tvCommit.setVisibility(View.GONE);
                            llJinxing.setVisibility(View.GONE);
                            tvChakan.setVisibility(View.VISIBLE);
                            break;
                        default:
                    }
                }catch(Exception e){

                }
            }
        });
    }

    @OnClick({R.id.fl, R.id.top_back, R.id.tv_chakan, R.id.tv_quxiao, R.id.ll_shangchuan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl:
                fl.setVisibility(View.GONE);
                break;
            case R.id.top_back:
                finish();
                break;
            case R.id.tv_chakan:
                Intent intent = new Intent(this, PictureUploadDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("item", new ScreenTaskBean(new ScreenTaskBean.DataBean(card.getData().getTaskid(), card.getData().getTitle(), 0, card.getData().getIsvip()), -1, card.getJin()));
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_quxiao:
                break;
            case R.id.ll_shangchuan:
                if (isvip == 1) {
                    if (UserBean.level == 0) {
                        final VipDIalog vipDIalog = new VipDIalog(CreditCardDetailsActivity.this);
                        vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                            @Override
                            public void chooseTrue() {
                                if (UserBean.gao > 0) {
                                    startActivity(new Intent(CreditCardDetailsActivity.this, PayVIPActivity.class));
                                } else {
                                    Toast.makeText(CreditCardDetailsActivity.this, "首次提现方能开通", Toast.LENGTH_SHORT).show();
                                }
                                vipDIalog.dismiss();
                            }
                            @Override
                            public void chooseFalse() {
                                vipDIalog.dismiss();
                            }
                        });
                        vipDIalog.show();
                    } else {
                        startActivityForResult(new Intent(this, PictureUploadActivity.class).putExtra("taskid", taskid)
                                .putExtra("examplepic", ""), 110);
                    }
                } else {
                    startActivityForResult(new Intent(this, PictureUploadActivity.class).putExtra("taskid", taskid)
                            .putExtra("examplepic", ""), 110);
                }
                break;
            default:
        }
    }

    private int getDimen(int dimeRes) {
        return (int) getResources().getDimension(dimeRes);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
